<template>
    <div class="cart-page">
        <!-- Header组件 -->
        <Header title="购物车"> 
            <template v-slot:left>
                 <span @click="$router.go(-1)">&lt;</span>  
             </template> 
        </Header>
        <div class="title">购物车</div>
        <div class="list" v-if="cartList.length > 0">
            <div class="item"  v-for="(item,index) in cartList" :key="index">
                <div class="pic">
                    <img :src="item.pic" alt="">
                </div>
                <div class="text">
                    <div class="name">{{item.name}}</div>
                    <div class="price">单价: ¥{{item.price}}</div>
                </div>
                <div class="btn" >
                    <span @click="sub(item)">-</span>
                    <span>{{item.count}}</span>
                    <span  @click="add_count(item.id)">+</span></div>
            </div>
        </div>
        <div class="empty" v-else>
            <img src="../../assets/images/没有更多.png" alt="">
            <div>购物车空空如也!</div>
        </div>
        <div class="total-bar">
            <div class="total"><span>总数:{{ cartList.reduce((total,item)=>{ return total + item.count } ,0) }}</span><span>总价:{{ cartList.reduce((total,item)=>{ return total + item.count*item.price } ,0) }}</span></div>
            <div class="toConfirm">去结算</div>
        </div>
    </div>
</template>

<script>
import { mapState ,mapMutations} from 'vuex'
import {MessageBox} from "mint-ui"
import 'mint-ui/lib/style.css'

/* 导入mapState辅助函数 */
export default {
    data(){
        return {

        }
    },
    computed:{
        ...mapState(['cartList'])
    },
    methods:{
        sub(item){
            if(item.count === 1)//数量为1,弹框确认
            {
                MessageBox.confirm("确定要删除吗?").then((action)=>{
                    this.sub_count(item.id)
                }).catch(()=>{

                });
            }
            else
            {
                this.sub_count(item.id)
            }
            
        },
        ...mapMutations(['add_count','sub_count'])
    }

}
</script>
<style scoped>

.title{
    line-height: 50px;
    padding-left: 20px;
    font-size: 24px;
}
.list .item{
    position: relative;
    display: flex;
    margin: 20px;
}
.list .item .pic{
    width: 25%;
    margin-right: 20px;
}
.list .item .pic img{
    width: 100%;
}
.list .item .text{
    width: 75%;
}
.list .item .text .name{
    color: #333;
    white-space: nowrap;/* 文本不换行 */
    text-overflow: ellipsis;/* 文本溢出 显示省略号 */
    overflow: hidden;/* 溢出盒子,隐藏 */
}
.list .item .btn{
    position: absolute;
    right: 0;
    bottom: 0;
    color: white;
    padding: 5px 20px;
}
.list .item .btn span:first-child,.list .item .btn span:last-child{
    padding: 0 10px;
    background-color: red;
    color: white;
}
.empty{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 50px 0;
}
.empty img{
    margin-bottom: 10px;
}

.total-bar{
    position: fixed;
    bottom: 0;
    width: 100%;
    display: flex;
    line-height: 50px;
    background-color: #888888;
    color:white;
}
.total-bar .total{
    padding-left: 20px;
    flex: 1;
}
.total-bar .total span{
    color: white;

}
.total-bar .total span:first-child{
    margin-right: 50px;
}
.total-bar .toConfirm{
    background-color: lightgreen;
    color: white;
    width: 100px;
    text-align: center;
}
</style>